﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using DevExpress.XtraGrid;
using DevExpress.XtraGrid.Views.Grid;
using DevExpress.XtraGrid.Columns;
using Model;

namespace TanHungLong
{
    public partial class FrmRMaVach : DevExpress.XtraEditors.XtraForm
    {
        #region variant
        private RMaVachBOCollection rMaVachBOColl = RMaVachBOCollection.NewRMaVachBOCollection();

        public static string _maVach = "0";
        public static int _khoID = -1;
        private int _MatHangID = -1;
        private int _DonViTinhID = 1;
        #endregion variant

        //=====================================
        #region form
        public FrmRMaVach(int _matHangID, int _donViTinhID)
        {
            InitializeComponent();
            this.KeyPreview = true;

            _MatHangID = _matHangID;
            _DonViTinhID = _donViTinhID;
            this.Load += new EventHandler(FrmRMaVach_Load);
            this.Shown += new EventHandler(FrmRMaVach_Shown);

            //this.btnRefresh.Click += new EventHandler(btnRefresh_Click);
            this.btnClose.Click += new EventHandler(btnClose_Click);
            this.grdView.KeyDown += new KeyEventHandler(this.grdView_KeyDown);
            this.grdView.Click += new EventHandler(this.grdView_Click);

        }

        private void FrmRMaVach_Shown(object sender, EventArgs e)
        {
            this.grdView.Focus();
        }

        private void FrmRMaVach_Load(object sender, EventArgs e)
        {
            RefreshData();
        }
        #endregion form

        //=====================================
        #region control
        #endregion control

        //=====================================
        #region button
        private void btnRefresh_Click(object sender, EventArgs e)
        {
            RefreshData();
        }

        private void btnClose_Click(object sender, EventArgs e)
        {
            this.Close();
        }
        #endregion button

        //=====================================
        #region grid

        #endregion grid

        //=====================================
        #region function
        private void RefreshData()
        {
            LayMaVach();
            this.bdSource.DataSource = this.rMaVachBOColl.Clone();
            #region

            #endregion BOList

        }

        private void LayMaVach()
        {
            DanhMucBOList khoBOList = DanhMucBOList.GetDanhMucBOList(LoaiDanhMuc.DanhMucKho);
            TonKhoBOCollection tonKhoBOColl = TonKhoBOCollection.GetTonKhoBOCollection("MatHangID = " + _MatHangID.ToString() + " and DonViTinhID = " + _DonViTinhID.ToString());

            MaVachBOCollection maVachBOColl = MaVachBOCollection.GetMaVachBOCollection();

            int rowcountMV = tonKhoBOColl.Count;
            if (rowcountMV > 0)

                for (int i = 0; i < rowcountMV; i++)
                {
                    int _tonKhoID = StaticUtil.GetInt(tonKhoBOColl[i].TonKhoID, -1);
                    int _KhoID = StaticUtil.GetInt(tonKhoBOColl[i].DanhMucKhoID, -1);
                    string _tenKho = GetTenKho(_KhoID, khoBOList);
                    int maVachCount = maVachBOColl.Count;
                    if (maVachCount > 0)
                    {
                        for (int j = 0; j < maVachCount; j++)
                        {
                            if (StaticUtil.GetInt(maVachBOColl[j].TonKhoID, -1) == _tonKhoID)
                            {
                                RMaVachBO rmaVachBO = RMaVachBO.NewRootRMaVachBO();
                                rmaVachBO.STT0 = i + 1;
                                rmaVachBO.MaVach = maVachBOColl[j].MaVach;
                                rmaVachBO.SoLuongTon = StaticUtil.GetDouble(maVachBOColl[j].SoLuongTon, 0);

                                rmaVachBO.KhoID = _KhoID;
                                rmaVachBO.TenKho = _tenKho;
                                rMaVachBOColl.Add(rmaVachBO);
                            }
                        }
                    }
                }
        }

        private string GetTenMatHang(int _matHangID, MatHangBOCollection matHangBOColl)
        {
            string ten = "";
            int reccordCount = matHangBOColl.Count;
            if (reccordCount > 0)

                for (int i = 0; i < reccordCount; i++)
                {
                    if (StaticUtil.GetInt(matHangBOColl[i].MatHangID, -1) == _matHangID)
                    {
                        ten = matHangBOColl[i].Ten;
                    }
                }

            return ten;
        }

        private string GetTenDonViTinh(int _donViTinhID, DanhMucBOList donViTinhBOList)
        {
            string ten = "";
            if (_donViTinhID >= 0)
                if (donViTinhBOList.ContainsKey(_donViTinhID))
                    ten = donViTinhBOList.Value(_donViTinhID);
                else
                    ten = "";
            return ten;
        }

        private string GetTenKho(int _khoID, DanhMucBOList khoBOList)
        {
            string ten = "";
            if (_khoID >= 0)
                if (khoBOList.ContainsKey(_khoID))
                    ten = khoBOList.Value(_khoID);
                else
                    ten = "";
            return ten;
        }
        #endregion function

        private void grdView_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyData == Keys.Enter)
            {
                try
                {
                    _maVach = this.grdView.GetFocusedRowCellValue("MaVach").ToString();
                    _khoID = StaticUtil.GetInt(this.grdView.GetFocusedRowCellValue("KhoID"));
                    this.Close();
                }
                catch {  };
            }

            if (this.btnClose.Visible && ((e.KeyCode == Keys.Q && Control.ModifierKeys == Keys.Control) || (e.KeyCode == Keys.F10)))
                this.Close();

        }

        private void grdView_Click(object sender, EventArgs e)
        {
            grdView_KeyDown(null, null);
        }
    }
}